<template>
  <div>
    <el-form :inline="true" :model="searchModel" class="demo-form-inline">
      <el-form-item label="字典名称">
        <el-input
          v-model="searchModel.searchItem.name"
          placeholder="字典名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="字典类型">
        <el-select v-model="searchModel.searchItem.type" placeholder="请选择">
            <el-option key="" label="请选择" value="">请选择</el-option>
          <el-option
            v-for="item in typeInfo"
            :key="item.dt_type_key"
            :label="item.dt_type_name"
            :value="item.dt_type_key"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="SearchPage">查询</el-button>
        <el-button type="primary" @click="openAdd">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.data" style="width: 100%">
      <el-table-column prop="dt_key" label="字典key"> </el-table-column>
      <el-table-column prop="dt_type_key" label="字典类型key">
      </el-table-column>
      <el-table-column prop="dt_type_name" label="字典类型名称">
      </el-table-column>
      <el-table-column prop="dt_name" label="字典名称"> </el-table-column>
      <el-table-column prop="dt_orderby" label="排序数字"> </el-table-column>
      <el-table-column prop="creator_name" label="创建人"> </el-table-column>
      <el-table-column prop="creator_date" label="创建时间"> </el-table-column>
      <el-table-column prop="modifi_name" label="修改人"> </el-table-column>
      <el-table-column prop="modifi_date" label="修改时间"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row.id)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageIndex"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
    >
    </el-pagination>

    <el-dialog
      title="字典添加"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="addinfo" label-width="80px">
        <el-form-item label="字典key">
          <el-input v-model="addinfo.dt_key"></el-input>
        </el-form-item>
        <el-form-item label="字典名称">
          <el-input v-model="addinfo.dt_name"></el-input>
        </el-form-item>
        <el-form-item label="字典类型">
        <el-select v-model="addinfo.dt_type_key" placeholder="请选择">
            <el-option key="" label="请选择" value="">请选择</el-option>
          <el-option
            v-for="item in typeInfo"
            :key="item.dt_type_key"
            :label="item.dt_type_name"
            :value="item.dt_type_key"
          >
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="addinfo.dt_orderby"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitAdd">立即创建</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="字典编辑"
      :visible.sync="dialogVisibles"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="addinfos" label-width="80px">
        <el-form-item label="字典key">
          <el-input v-model="addinfos.dt_key"></el-input>
        </el-form-item>
        <el-form-item label="字典名称">
          <el-input v-model="addinfos.dt_name"></el-input>
        </el-form-item>
        <el-form-item label="字典类型">
        <el-select v-model="addinfos.dt_type_key" placeholder="请选择">
            <el-option key="" label="请选择" value="">请选择</el-option>
          <el-option
            v-for="item in typeInfo"
            :key="item.dt_type_key"
            :label="item.dt_type_name"
            :value="item.dt_type_key"
          >
          </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="addinfos.dt_orderby"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitUpdate">立即修改</el-button>
          <el-button @click="dialogVisibles = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

  <script>
import {
  DictionaryTableAdd,
  DictionaryTableDelete,
  DictionaryTableEdit,
  DictionaryTableUpdate,
  SearchPage,
  BindType,
} from "./DictionaryTableApi";
export default {
  data() {
    return {
      typeInfo: {},
      dialogVisible: false,
      addinfo: {},
      dialogVisibles: false,
      addinfos: {
        id: null,
      },
      tableData: [],
      searchModel: {
        searchItem: {
          name: null,
          type: null,
        },
        pageIndex: 1,
        pageSize: 3,
      },
    };
  },
  created() {
    this.SearchPage();
    this.BindTypes();
  },
  methods: {
    BindTypes() {
      BindType().then((r) => {
        console.log(r);
        this.typeInfo = r.data;
      });
    },
    onSubmitUpdate() {
      DictionaryTableUpdate(this.addinfos).then((r) => {
        if (r.data) {
          this.$message("修改成功!");
          this.dialogVisibles = false;
          this.SearchPage();
        }
      });
    },
    handleEdit(index, row) {
      this.addinfos.id = row;
      this.dialogVisibles = true;
      this.BindTypes();
      DictionaryTableEdit(row).then((r) => {
        this.addinfos = r.data;
      });
    },
    openAdd() {
      this.addinfo = {};
      this.dialogVisible = true;
      this.BindTypes();
    },
    onSubmitAdd() {
      DictionaryTableAdd(this.addinfo).then((r) => {
        if (r.data) {
          console.log(r);
          this.$message("添加成功!");
          this.dialogVisible = false;
          this.SearchPage();
        }
      });
    },
    SearchPage() {
      SearchPage(this.searchModel).then((r) => {
        //  console.log(r.data.data);
        this.tableData = r.data;
      });
    },
    handleSizeChange(val) {
      this.searchModel.pageSize = val;
      this.searchModel.pageIndex = 1;
      this.SearchPage();
    },
    handleCurrentChange(val) {
      this.searchModel.pageIndex = val;
      this.SearchPage();
    },
    handleDelete(index, row) {
      console.log(row);
      if (confirm("确认要删除吗")) {
        DictionaryTableDelete(row).then((r) => {
          if (r.data) {
            this.$message("删除成功!");
            this.SearchPage();
          }
        });
      }
    },
  },
};
</script>